import { defineStore } from "pinia";
import type { Theme } from "@@/typeModule/AppModule";
import { GetWebSiteLogo } from "~/apis/web";
const themeConfig: Theme = {
  sidebarStyle: "dark",
  headerStyle: "light",
  primaryColor: "#1F93FF",
  uniqueOpened: true,
  isLogo: true,
  isBreadcrumb: true,
  isTabsView: false,
  isTabsCache: false,
  tabsStyle: "style-1",
};

export const appStore = defineStore("appStore", {
  state: () => ({
    // sidebar 是否展开
    sidebarOpened: true,
    // 国际化
    language: "zh-HK",
    // 组件大小
    componentSize: "default",
    // 主题
    theme: themeConfig,
    // 站点信息
    siteInfo: null as any | null,
  }),
  getters: {
    siteLogo: (state) => state.siteInfo?.imageUrl,
    siteName: (state) => state.siteInfo?.outsourcerName,
    mobileCode: (state) => state.siteInfo?.mobileCode,
    customer: (state) => ({
      name: state.siteInfo?.customer,
      code: state.siteInfo?.customerCode,
      phone: state.siteInfo?.contactPhone,
      email: state.siteInfo?.contactEmail,
    }),
  },
  actions: {
    setSidebarOpened() {
      this.sidebarOpened = !this.sidebarOpened;
    },
    setLanguage(localeVla: any) {
      this.language = localeVla;
    },
    setComponentSize(size: string) {
      this.componentSize = size;
    },
    setTheme(theme: Theme) {
      this.theme = theme;
    },
    async updateSiteInfo(tenantId: String | number) {
      const { code, data } = await GetWebSiteLogo({ tenantId });
      if (code == 0) {
        this.siteInfo = data;
        if (data?.configuration) {
          const nowTheme = this.theme;
          nowTheme.primaryColor = data.configuration;
          this.setTheme(nowTheme);
        }
      }
    },
  },
  persist: true,
});
